#include "cmsis_os.h"
#include "adc_monitor.h"
#include "adc.h"
#include "tim.h"
#include <math.h>
#include "axis.h"
#include "changeable_values.h"

float V_BUS(uint16_t adc){
	static float temp1= V_BUS_MAX/((V_R1+V_R2)/V_R2)/V_REF;
	float temp2 = 4096*temp1/V_BUS_MAX;
	
	return (float)(adc/temp2);
}

float temp_trans(uint16_t adc)
{
   float T2=273.15f+TEMP0;
   float vol=(float)(adc*(3.3f/4096));
   float Rt = (3.3f-vol)*10000/vol;
   float temp = 1/(1/T2+log(Rt/NTC_R)/B_VALUE)-K_VALUE+0.5f;
	return temp;
}

void adc_monitor_task(){
	uint32_t ADC_VALUE[3]={0,2600,0};
//	HAL_ADC_Start(&hadc1);

	for(;;){
		HAL_ADC_Start_DMA(&hadc1, ADC_VALUE, 2);
		axis1.drive.Bus_Voltage = V_BUS(ADC_VALUE[0]);
		axis1.drive.Mos_Temperature=temp_trans(ADC_VALUE[1]);
		vTaskDelay(1000);
	}
}

